<?php

require_once('functions/pageLoad.php');

$page_title = 'Refer a friend';

require_once('functions/recaptchalib.php');
$publickey = "6LdLhAYAAAAAABwx-1kiacx5VqTj5YWFnanQE3NS";
$privatekey = "6LdLhAYAAAAAABdiW_7-udhTrdGURvR6XtM9eUTe";

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form'] == 'refer'){

$email_regex = "/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/i";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

$fail.= (strlen($_POST['users_name']) == 0) ? '<li>Your name was not entered</li>' : '';
$fail.= (preg_match($email_regex, $_POST['users_email']) == 0) ? '<li>Your email address is invalid</li>' : '';
$fail.= (strlen($_POST['friends_name']) == 0) ? '<li>Your friends name was not entered</li>' : '';
$fail.= (preg_match($email_regex, $_POST['friends_email']) == 0) ? '<li>Your friends email address is invalid</li>' : '';
$fail.= (!$resp->is_valid) ? '<li>Incorrect ReCaptcha phrase entered</li>' : '';

if(strlen($fail) > 0){

// PHAIL

$results = '<p style="font-weight: bold; color: #cc0000;">Please correct the following errors;</p><ul style="font-weight: bold; color: #cc0000;">'.$fail.'</ul>';

}

else
{

// FINE - INSERT, SEND EMAIL AND REDIRECT

	require("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['no_reply_email'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($_POST['friends_email']);
	$mail->AddAddress($_POST['users_email']);

	$mail->Subject = "I Want A Tradesman";
	$mail->Body = "Dear ".$_POST['friends_name']."\r\n\r\nYour friend: ".$_POST['users_name']." wanted to let you know about the new \"I Want A Tradesman\" system. Where you can post trade jobs for local tradesman to quote for. We have everything from Alarm fitters to Window fitters and best of all its completely free to obtain competitive quotes from trusted tradesmen!\r\n\r\nCheck it out: http://www.iwantatradesman.co.uk\r\n\r\nAlternatively, if you're a tradesman you can be a part of the team by registering your business for consideration. Simply complete the business registration form on the website and we'll put you in our database of local tradesman!\r\n\r\nHope to see you there soon!\r\n\r\nKind regards\r\n\r\nThe I Want A Tradesman Team\r\n\r\n\r\nThis email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.";

	$mail->Send();

$results = '<p style="font-weight: bold; color: #009900;">Thank you! We have sent your a friend a link to I Want A Tradesman.</p>';

$_POST['users_name'] = '';
$_POST['users_email'] = '';
$_POST['friends_name'] = '';
$_POST['friends_email'] = '';

}

}

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1 id="bigWhiteHeader">Refer a friend</h1>

<div id="leftColumn">
<?php
$banner_area_type = 'visitor_area';
echo get_banner_ad('content_banner', $banner_area_type);
?>

<?php echo $results; ?>

<p>Are you impressed with the website? Refer a friend to <strong>I Want A Tradesman</strong> today and let them know. We will send them an email on your behalf with a link to the website.</p>

<form method="post" action="/refer-a-friend.php" id="refer">

<input type="hidden" id="form" name="form" value="refer" style="display: none;" />

<div id="registration_form">

<div class="formAreaWrap">

<div class="formRowWrapper" id="users_name_wrap">
<div class="formRowLeft"><label for="users_name">Your name:</label></div>
<div class="formRowMiddle"><input type="text" name="users_name" id="users_name" class="textbox" onfocus="return highlight('users_name');" onblur="return removeHighlight('users_name');" value="<?php echo $_POST['users_name']; ?>" /></div>
<div class="formRowRight" id="users_name_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="users_email_wrap">
<div class="formRowLeft"><label for="users_email">Your email:</label></div>
<div class="formRowMiddle"><input type="text" name="users_email" id="users_email" class="textbox" onfocus="return highlight('users_email');" onblur="return removeHighlight('users_email');" value="<?php echo $_POST['users_email']; ?>" /></div>
<div class="formRowRight" id="users_email_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="friends_name_wrap">
<div class="formRowLeft"><label for="friends_name">Your friends name:</label></div>
<div class="formRowMiddle"><input type="text" name="friends_name" id="friends_name" class="textbox" onfocus="return highlight('friends_name');" onblur="return removeHighlight('friends_name');" value="<?php echo $_POST['friends_name']; ?>" /></div>
<div class="formRowRight" id="friends_name_result"></div>
</div>

</div><div class="formAreaWrap">

<div class="formRowWrapper" id="friends_email_wrap">
<div class="formRowLeft"><label for="friends_email">Your friends email:</label></div>
<div class="formRowMiddle"><input type="text" name="friends_email" id="friends_email" class="textbox" onfocus="return highlight('friends_email');" onblur="return removeHighlight('friends_email');" value="<?php echo $_POST['friends_email']; ?>" /></div>
<div class="formRowRight" id="friends_email_result"></div>
</div>

</div>

<div class="formRowWrapper">
<div class="formRowLeft">&nbsp;</div>
<div class="formRowMiddle" style="width: 450px; font-size: 16px;"><?php echo recaptcha_get_html($publickey); ?></div>
</div>

<div class="formRowWrapper">
<div class="formRowLeft">&nbsp;</div>
<div class="formRowMiddle">
<script type="text/javascript">document.write('<'+'a onclick="return formval(\'refer\');" style="cursor: pointer;"><'+'img src="/images/submit_button.gif" alt="Submit" /><'+'/a>');</script>
<noscript><input type="submit" value="Register" class="blue_submit_button" /></noscript></div>
</div>

</div>
</form>

</div>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>